<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <input type="button" value="download" id="download">
    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>

        function setCookie(name, value) {
            var Days = 30;
            var exp = new Date();
            exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
            document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
        }

        setCookie("token", "1234567890");
        // axios.defaults.withCredentials = true;

        document.querySelector("#download").addEventListener("click", function (e) {
            // window.open("https://localhost:18080/sc1902/logMngm/exportExl","_blank");
            // axios({
            //     method: 'get',
            //     // url: 'https://172.31.4.248:18080/sc1902/logMngm/exportExl',
            //     url: 'https://localhost:18080/sc1902/logMngm/exportExl',
            //     data: {
            //         "startDate": "2019-10-08 09:00:00",
            //         "endDate": "2019-10-11 09:00:00"
            //     },
            //     responseType: 'blob'
            // }).then(function (response) {
            //     console.log(response);
            //     createDownload("操作日志.xls", response.data)
            // }).catch(function (error) {
            //     console.log(error);
            // });


            axios.post("2.php", {
                "filename": "1.txt",
                "startDate": "2019-07-08 09:00:00",
                "endDate": "2019-10-11 09:00:00"
            }, {
                withCredentials: false,
                responseType: 'arraybuffer'
            }).then(res => {
                console.log(res);

                const data = res.data
                const url = window.URL.createObjectURL(new Blob([data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" }))
                const link = document.createElement('a')
                link.style.display = 'none'
                link.href = url
                link.setAttribute('download', 'excel.xlsx')
                document.body.appendChild(link)
                link.click()
                document.body.removeChild(link)

                // let blob = res.data
                // let reader = new FileReader()
                // reader.readAsDataURL(blob)
                // reader.onload = (e) => {
                //     let a = document.createElement('a')
                //     a.download = "操作日志.xls"
                //     a.href = e.target.result
                //     document.body.appendChild(a)
                //     a.click()
                //     document.body.removeChild(a)
                // }
            })


            // fetch("https://localhost:18080/sc1902/logMngm/exportExl", {
            //     method: "POST",
            //     headers:{
            //         "Content-type":"application/json"
            //     },
            //     body: JSON.stringify({
            //         "startDate": "2019-10-08 09:00:00",
            //         "endDate": "2019-10-11 09:00:00"
            //     })
            // }).then(resp => {
            //     console.log(resp);
            //     return resp.blob().then(data => {

            //         createDownload("操作日志.xls", data)
            //     });
            // })

        });
        function createDownload(fileName, content) {
            // var blob = new Blob([content]);
            var blob = content;
            var link = document.createElement("a");
            link.innerHTML = fileName;
            link.download = fileName;
            link.href = URL.createObjectURL(blob);
            document.getElementsByTagName("body")[0].appendChild(link);
        }
    </script>
</body>

</html>